package controllers;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Date;

import service.BorrowService;
import models.Borrow;
import models.Indent;
import models.LendCard;
import models.LendCardF;
import models.Member;

@CRUD.For(Borrow.class)
public class Borrows extends CRUD {
	
	public static void fborrow(String id){
		Indent indlist = BorrowService.findborrow(id);
		render("/Indents/Iborrow.html",indlist,id);
	}
	
	public static void upbrotime(String brtime,Long brid,String ordid){
		int state = 0 ;
		try{
			Indent indent = Indent.findById(Long.parseLong(ordid));
			System.out.println(indent+"tttttttttttttttttttttttttt"+brtime+"|||||||||||||"+brid);
			Member member = Member.findById(Long.parseLong(indent.memid));
			System.out.println(member.lecnum+"-----------------------------------------------");
			LendCard lendcard = LendCard.find("from LendCard where lecnum = ?", member.lecnum).first() ;
			System.out.println(lendcard+"-----------------------------------------------");
			int dengji = (int) (lendcard.lecmone/50);
			int dd= dengji/2 ;
			int day = 0 ;
			if(dd == 0.5){
				day = 20 ;
			}
			if(dd == 1){
				day = 20 ;
			}
			if(dd == 2){
				day = 30 ;
			}
			if(dd == 3){
				day = 40 ;
			}
			if(dd == 4){
				day = 50 ;
			}
			System.out.println(day+"-------------------");
			Borrow borrow = Borrow.findById(brid);
		
			SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
			
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
			
			System.out.println(brtime+"----------------------------ssss");
			//String edtime = format1.format(formatter.parse(brtime));
	        Date tt = formatter.parse(brtime);
	        System.out.println(tt);
			
			borrow.bortime = tt ;
			Calendar cal=Borrows.changeStringToDate(format2.format(tt));
			cal.add(Calendar.DAY_OF_MONTH, day);
			System.out.println(day+"-------------------");
			String newString=Borrows.getDateString(cal);
			System.out.println(newString+"--------nn--------------");
			
			java.util.Date btime= format2.parse(newString);
			borrow.borsbt = btime ;
			borrow.save() ;
			state = 1 ;
			render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
	
	//把字符串类型的转成Calendar类型
	public static Calendar changeStringToDate(String date){
		String regex="-";
		String d[]=date.split(regex);
		//你这里进行分隔了 比如说2012-05-02 11：11：11 分隔后的是2012，
		int year=Integer.parseInt(d[0]);
		int month=Integer.parseInt(d[1])-1;
		int day=Integer.parseInt(d[2]);
		Calendar calendar=Calendar.getInstance();
		calendar.set(year,month,day);
		return calendar;
	}
	
	//把日期格式转成字符串形式
	public static String getDateString(Calendar cal){
		int year=cal.get(Calendar.YEAR);
		int month=cal.get(Calendar.MONDAY)+1;
		int day=cal.get(Calendar.DAY_OF_MONTH);
		return year+"-"+month+"-"+day;
	}
}
